package org.yar.common;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EncodingUtils {
	private static String [] encodings = null;
	public static String [] getEncodings()
	{
		if (encodings != null)
		{
			return encodings;
		}
		
		Map<?, ?> encMap = Charset.availableCharsets();
		List<String> list = new ArrayList<String>();
		
		for (Object o: encMap.keySet())
		{
			String enc = (String)o;
			list.add(enc);
		}
		
		encodings = list.toArray(new String [] { });
		
		return encodings;
	}
	
	public static boolean isValidEncoding(String enc)
	{
		String [] encs = getEncodings();
		for (String e: encs)
		{
			if (enc.equals(e))
			{
				return true;
			}
		}
		return false;
	}
}
